<?php

class ReplyModel extends DBModel
{
	protected $_table = '{{reply}}';
	protected $_id = 'replyid';
	
	public function getListByReplyerId($replyerid, $replytype, $pageno, $pagesize, $select = '*', $itemid = null)
	{
		$where['from'] = '{{reply}} r';
		//$where['select'] = 'r.replyerid,u.truename,u.sign,p.midpath logopath';
		$where['select'] = $select;
		$where['condition'] = 'r.replyerid = :replyerid';
		$where['join'] = 'left join {{user}} u on u.userid = r.replyerid
						  left join {{photo}} p on u.logoid = p.photoid';
		$where['limit'] = $pagesize;
		$where['offset'] = max(0, $pageno - 1) * $pagesize;
		$params = array(':replyerid' => $replyerid);
		if ($replytype !== false) {
			$where['condition'] .= ' AND r.replytype = :replytype';
			$params[':replytype'] = $replytype;
		}
		if ($itemid !== null) {
			$where['condition'] .= ' AND r.itemid = :itemid';
			$params[':itemid'] = $itemid;
		}
		$where['order'] = 'r.replytime DESC';
		return $this->findAll($where, $params);
	}
	
	public function getListByUserId($userid, $replytype, $pageno, $pagesize, $select = '*')
	{
		$where['from'] = '{{reply}} r';
		$where['select'] = $select;
		$where['condition'] = 'r.userid = :userid';
		$where['join'] = 'left join {{user}} u on u.userid = r.replyerid
						  left join {{photo}} p on u.logoid = p.photoid';
		$where['limit'] = $pagesize;
		$where['offset'] = max(0, $pageno - 1) * $pagesize;
		$params = array(':userid' => $userid);
		if ($replytype !== false) {
			$where['condition'] .= ' AND r.replytype = :replytype';
			$params[':replytype'] = $replytype;
		}
		$where['order'] = 'r.replytime DESC';
		return $this->findAll($where, $params);
	}
	
	public function getListByUserId1($userid, $replytype, $pageno, $pagesize, $newtype, $select = '*')
	{
		$limit = $pagesize;
		$offset = max(0, $pageno - 1) * $pagesize;
// 		if ($newtype == 1) {
// 			$where_itemid = ' and itemid = 0';
// 		} elseif ($newtype == 2) {
// 			$where_itemid = ' and itemid > 0';
// 		}

		if ($newtype == 2) {
			$where_itemid = ' and itemid > 0';
		} else {
			$where_itemid = ' and itemid = 0';
		}
		$sql = 'select * 
				from reply 
				where replyid in (
					select max(replyid) from reply where userid = :userid '.$where_itemid.' group by userid order by replytime
				) 
				union 
				select * from reply where replyerid = :userid '.$where_itemid.' and userid not in (
					select max(replyerid) from reply where userid = :userid group by userid order by replytime
				) group by userid 
				order by replytime desc 
				limit ' . $offset . ',' . $limit;
		$params[':userid'] = $userid;
		return $this->queryBySql($sql, $params, 'queryAll');
		/*
		$where['from'] = '{{reply}}';
		$where['select'] = $select;
		$where['condition'] = 'userid = :userid or replyerid = :userid';
		$where['limit'] = $pagesize;
		$where['offset'] = max(0, $pageno - 1) * $pagesize;
		$params = array(':userid' => $userid, ':replyerid' => $userid);
		if ($replytype !== false) {
			$where['condition'] .= ' AND replytype = :replytype';
			$params[':replytype'] = $replytype;
		}
		$where['order'] = 'replytime DESC';
		return $this->findAll($where, $params);
		*/
	}
	
	public function getListSupportMe($userid, $replytype, $pageno, $pagesize, $select = '*', $itemid = null)
	{
		$where['from'] = '{{reply}} r';
		$where['select'] = $select;
		$where['condition'] = 'r.userid = :userid';
		$where['join'] = 'left join {{user}} u on u.userid = r.replyerid
						  left join {{photo}} p on u.logoid = p.photoid';
		$where['limit'] = $pagesize;
		$where['offset'] = max(0, $pageno - 1) * $pagesize;
		$params = array(':userid' => $userid);
		if ($replytype !== false) {
			$where['condition'] .= ' AND r.replytype = :replytype';
			$params[':replytype'] = $replytype;
		}
        if ($itemid !== null) {
            $where['condition'] .= ' AND r.itemid = :itemid';
            $params[':itemid'] = $itemid;
        }
		$where['order'] = 'howmuch DESC';
		$where['group'] = 'r.replyerid';
		return $this->findAll($where, $params);
	}
	
	public function getListAll($userid, $replyerid, $pageno, $pagesize, $select = '*')
	{
		$sql = "SELECT * FROM (
					SELECT {$select} FROM  {{reply}} r 
					LEFT JOIN {{user}} u on u.userid = r.replyerid 
					LEFT JOIN {{photo}} p on u.logoid = p.photoid 
					WHERE (r.userid = :userid AND r.replyerid = :replyerid) OR (r.userid = :replyerid AND r.replyerid = :userid) AND r.replytype = :replytype ORDER BY replytime DESC LIMIT 0,50 
				) AS t ORDER BY t.createtime ASC";
// 		$where['from'] = '{{reply}} r';
// 		$where['select'] = $select;
// 		$where['condition'] = '(r.replyerid = :replyerid and r.userid = :userid) or (r.replyerid = :userid and r.userid = :replyerid) and r.replytype = :replytype';
// 		$where['join'] = 'left join {{user}} u on u.userid = r.replyerid
// 						  left join {{photo}} p on u.logoid = p.photoid';
// 		$where['limit'] = $pagesize;
// 		$where['offset'] = max(0, $pageno - 1) * $pagesize;
		$params = array(':userid' => $userid, 'replyerid' => $replyerid, ':replytype' => 2);
		return $this->queryBySql($sql, $params, 'queryAll');
	}
	
	public function getListByItemId($itemid, $userid, $replytype, $pageno, $pagesize, $select = '*')
	{
		$where['from'] = '{{reply}} r';
		$where['select'] = $select;
		$where['condition'] = 'r.itemid = :itemid AND r.replytype = :replytype';
		$params = array(':itemid' => $itemid, ':replytype' => $replytype);
		if ($userid) {
			$where['condition'] .= ' AND r.userid = :userid';
			$params[':userid'] = $userid;
		}
		$where['join'] = 'left join {{user}} u on u.userid = r.replyerid
						  left join {{photo}} p on u.logoid = p.photoid';
		$where['limit'] = $pagesize;
		$where['offset'] = max(0, $pageno - 1) * $pagesize;
		$where['order'] = 'r.replytime DESC';
		return $this->findAll($where, $params);
	}
	
	public function updateNewsNum($userid)
	{
		$condition = 'userid = :userid';
		$params = array(':userid' => $userid);
		return $this->update(array('isnew' => 0), $condition, $params);
	}
	
	public function resetNewsNum($userid)
	{
		$condition = 'userid = :userid';
		$params = array(':userid' => $userid);
		return $this->update(array('isnew' => 0), $condition, $params);
	}
	
	public function getNewsNum($userid)
	{
		$where['select'] = 'COUNT(*)';
		$where['condition'] = 'userid = :userid and isnew = 1';
		$params = array(':userid' => $userid);
		return $this->findScalar($where, $params);
	}
	
	public function getCountByItemID($itemid, $replytype = false)
	{
		$where['select'] = 'COUNT(*)';
		$where['condition'] = 'itemid = :itemid';
		$params = array(':itemid' => $itemid);
		if ($replytype !== false) {
			$where['condition'] .= ' AND replytype = :replytype';
			$params[':replytype'] = $replytype;
		}
		return $this->findScalar($where, $params);
	}
	
	public function getSumMuch($buserid, $itemid)
	{
		$where['select'] = 'SUM(howmuch)';
		$where['condition'] = 'replyerid = :replyerid AND itemid = :itemid';
		$params[':replyerid'] = $buserid;
		$params[':itemid'] = $itemid;
		return $this->findScalar($where, $params);
	}
	
	public function getSumMuchByUserId($userid, $itemid)
	{
		$where['select'] = 'SUM(howmuch)';
		$where['condition'] = 'userid = :userid AND itemid = :itemid';
		$params[':userid'] = $userid;
		$params[':itemid'] = $itemid;
		return $this->findScalar($where, $params);
	}
	
	public function getStateByItemId($item_id)
	{
		//$where['select'] = 'sum(howmuch) suportmoney, count(*) suportcount';
		$where['select'] = 'count(*) suportcount';
		$where['condition'] = 'itemid = :itemid AND replytype = :replaytype';
		$where['group'] = 'itemid';
		$params = array(':itemid' => $item_id, ':replaytype' => 1);
		return $this->find($where, $params);
	}
	
	public function getInfoByBoth($userid, $replyerid, $select = '*')
	{
		$where['select'] = $select;
		$where['condition'] = '(userid = :userid AND replyerid = :replyerid) OR (userid = :replyerid AND replyerid = :userid)';
		$where['order'] = 'replytime desc';
		$params = array(':userid' => $userid, ':replyerid' => $replyerid);
		return $this->find($where, $params);
	}
}